﻿using System.Collections.Generic;
using System.Linq;
using System;

namespace Wwtfly.BBS.Framework.Common
{
    /// <summary>
    /// 分页 传递参数
    /// </summary>
    public class PageParams
    {
        /// <summary>
        /// 每页显示记录
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalCount { get;private set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages { get; private set; }
        
        /// <summary>
        /// 排序 字段
        /// </summary>
        public string SortFieldName { get; set; }

        /// <summary>
        /// 排序方向
        /// </summary>
        public SortDirectionEnum SortDirection { get; set; }

        /// <summary>
        /// 分页 大小集合
        /// </summary>
        public int[] PageSizeList = new int[] { 10, 15, 20, 100 };

        /// <summary>
        /// 构造函数
        /// </summary>
        public PageParams()
        {
        }

         /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pageParams">分页参数传递字符串</param>
        public PageParams(string pageParams)
        {
            try
            {

                string[] paramses = pageParams.Split('-');
                this.PageIndex = int.Parse(paramses[0]);
                if (PageSizeList.Contains(int.Parse(paramses[1])))
                {
                    this.PageSize = int.Parse(paramses[1]);
                }
                else
                {
                    this.PageSize = PageSizeList[0];
                }
                this.SortDirection = (SortDirectionEnum)int.Parse(paramses[2]);
            }
            catch
            {
                //throw new Exception("传入的参数有误, 请联系管理员!");
                this.PageIndex = 1;
                this.PageSize = PageSizeList[0];
            }
        }


        /// <summary>
        /// 重写toString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0}-{1}-{2}",
                 this.PageIndex,
                this.PageSize,
                (int)this.SortDirection
                );
        }



        /// <summary>
        /// 输出 pageParams
        /// </summary>
        /// <returns></returns>
        public  string ToString(int pageIndex, int pageSize, SortDirectionEnum sort)
        {
            return string.Format("{0}-{1}-{2}",
                 pageIndex,
                pageSize,
                (int)sort
                );
        }

        /// <summary>
        /// 设置 总条数.. 并计算总页数
        /// </summary>
        /// <param name="totalCount"></param>
        public void SetTotalCount(int totalCount)
        {
            this.TotalCount = totalCount;
            if (this.PageSize != 0)
            {
                this.TotalPages = TotalCount / PageSize;

                if (TotalCount % PageSize > 0)
                    TotalPages++;
            }
        }

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool IsPreviousPage
        {
            get { return ((PageIndex - 1) > 0); }
        }

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool IsNextPage
        {
            get { return (PageIndex * PageSize) < TotalCount; }
        }



    }
}
